package utils.reader.support;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import utils.reader.ResourceReader;
import utils.resource.Resource;

/**
 * 资源读取器模版方法
 * @author 曹炳俊
 *
 */
public abstract class AbstractResourceReader implements ResourceReader {
	
	protected InputStream inputStream;
	protected Resource resource;
	
	/**
	 * 读取资源文件
	 */
	public Resource read(String path) {
		loadResource(path);
		packageResource(path);
		closeResource();
		return this.resource;
	}
	
	/**
	 * 载入资源
	 * @param path
	 */
	private void loadResource(String path){
		try {
			this.inputStream = new FileInputStream(path);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 关闭资源
	 */
	private void closeResource(){
		try {
			if (this.inputStream != null) {
				this.inputStream.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 打包成一个什么样的资源让子类实现
	 */
	public abstract void packageResource(String path);
}
